using System.Collections;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class PlayerCareerLayer : MonoBehaviour
{
	public CoachMessageBoard coachBoard;

	public PlayerCareerDialog dialog;

	public TournamentCareerDialog tournamentDialog;

	public TournamentLayer tournamentLayer;

	public GameObject buttonPrefab;

	public GameObject tournamentPrefab;

	public GameObject groupPrefab;

	public GameObject scrollView;

	public GameObject linePrefab;

	public Transform container;

	public bool shouldShowFreeAppWelcome;

	public GameObject multiplayerArrow;

	protected List<PlayerCareerButton> buttons = new List<PlayerCareerButton>();

	protected List<StageGroupLine> groupLines = new List<StageGroupLine>();

	protected List<UISprite> lines = new List<UISprite>();

	public PlayerCareerStages stageDefinition
	{
		get
		{
			return ScriptableObjectSingleton<PlayerCareerStages>.instance;
		}
	}

	public void Awake()
	{
		Init();
	}

	public void Init()
	{
		List<PlayerCareerButton> list = new List<PlayerCareerButton>();
		List<PlayerCareerButton> list2 = new List<PlayerCareerButton>();
		Time.timeScale = 1f;
		PlayerCareerButton playerCareerButton = null;
		int num = 0;
		PlayerCareer instance = SingletonInit<PlayerCareer>.Instance;
		int num2 = 0;
		int lineIndex = 0;
		coachBoard.Hide();
		List<PlayerCareerStageDefinition.StageDefinition> list3 = new List<PlayerCareerStageDefinition.StageDefinition>();
		list3.AddRange(stageDefinition.stages);
		list3.AddRange(stageDefinition.optionalStages);
		int num3 = (int)list3[0].position.x;
		int num4 = (int)list3[0].position.x;
		Color color = Color.blue;
		foreach (PlayerCareerStageDefinition.StageDefinition item in list3)
		{
			PlayerCareerButton playerCareerButton2 = null;
			if (num2 >= buttons.Count)
			{
				GameObject prefab = ((item.subStages.Count <= 0) ? buttonPrefab : tournamentPrefab);
				buttons.Add(NGUITools.AddChild(container.gameObject, prefab).GetComponent<PlayerCareerButton>());
				playerCareerButton2 = buttons[num2];
				playerCareerButton2.transform.localPosition = item.position;
				playerCareerButton2.gameObject.SetActive(true);
			}
			PlayerCareerStageDefinition.StageGroupDefinition group = item.prevGroup.group;
			PlayerCareerStageDefinition.StageGroupDefinition group2 = item.nextGroup.group;
			if (group != null)
			{
				color = group.color;
			}
			playerCareerButton2 = buttons[num2];
			num2++;
			if (item.isOptional)
			{
				list2.Add(playerCareerButton2);
			}
			else
			{
				list.Add(playerCareerButton2);
			}
			if (item.isOptional)
			{
				List<PlayerCareerButton> list4 = ((item.prevStage.listType != 0) ? list2 : list);
				playerCareerButton = list4[item.prevStage.index];
				color = playerCareerButton.buttonActiveColor;
			}
			num3 = Mathf.Min(num3, (int)item.position.x);
			num4 = Mathf.Max(num4, (int)item.position.x);
			playerCareerButton2.buttonActiveColor = color;
			playerCareerButton2.Init(item, num, this);
			if (item.isActive && item.isCurrent && !string.IsNullOrEmpty(item.coachSays))
			{
				coachBoard.Show(item.coachSays, item.position);
			}
			if (group2 != null)
			{
				lineIndex = DrawLine(lineIndex, item.isActive, color, playerCareerButton2.transform.parent.TransformPoint(group2.position), playerCareerButton2.transform.position);
			}
			if (group != null)
			{
				lineIndex = DrawLine(lineIndex, item.isActive, color, playerCareerButton2.transform.parent.TransformPoint(group.position), playerCareerButton2.transform.position);
			}
			else if (playerCareerButton != null)
			{
				lineIndex = DrawLine(lineIndex, item.isActive, color, playerCareerButton.transform.position, playerCareerButton2.transform.position);
			}
			playerCareerButton = playerCareerButton2;
			if (!item.isOptional)
			{
				num++;
			}
		}
		num2 = 0;
		foreach (PlayerCareerStageDefinition.StageGroupDefinition stageGroup in ScriptableObjectSingleton<PlayerCareerStages>.instance.stageGroups)
		{
			StageGroupLine stageGroupLine = null;
			if (num2 >= groupLines.Count)
			{
				groupLines.Add(NGUITools.AddChild(container.gameObject, groupPrefab).GetComponent<StageGroupLine>());
			}
			stageGroupLine = groupLines[num2];
			PlayerCareerStageDefinition.StageGroupDefinition stageGroupDefinition = stageGroup;
			if (num2 + 1 < ScriptableObjectSingleton<PlayerCareerStages>.instance.stageGroups.Count)
			{
				stageGroupDefinition = ScriptableObjectSingleton<PlayerCareerStages>.instance.stageGroups[num2 + 1];
			}
			stageGroupLine.Init(stageGroup, num3, num4, Mathf.Abs(stageGroupDefinition.position.y - stageGroup.position.y));
			num2++;
		}
		PlayerCareerButton playerCareerButton3 = list[Mathf.Clamp(SingletonInit<PlayerCareer>.Instance.maxStageReached, 0, list.Count - 1)];
		StopAllCoroutines();
		StartCoroutine(UITools.DoCenterOnScroll(playerCareerButton3.transform, scrollView.GetComponent<UIScrollView>()));
	}

	public int DrawLine(int lineIndex, bool isActive, Color color, Vector3 position1, Vector3 position2)
	{
		UISprite uISprite = null;
		if (lineIndex >= lines.Count)
		{
			lines.Add(NGUITools.AddChild(container.gameObject, linePrefab).GetComponent<UISprite>());
			uISprite = lines[lineIndex];
			uISprite.cachedGameObject.SetActive(true);
			UITools.Line(uISprite, position1, position2);
		}
		uISprite = lines[lineIndex];
		lineIndex++;
		uISprite.color = Color.Lerp(color, Color.black, (!isActive) ? 0.75f : 0.25f);
		uISprite.alpha = ((!isActive) ? 0.5f : 1f);
		return lineIndex;
	}

	public void PassStage()
	{
		PlayerCareer instance = SingletonInit<PlayerCareer>.Instance;
		List<PlayerCareerStageDefinition.StageDefinition> stages = stageDefinition.stages;
		if (instance.scoreDAO.maxStageReached < stageDefinition.stages.Count)
		{
			CareerScoreDAO.StageScore stageScore = instance.GetStageScore(stages[instance.maxStageReached].stageId);
			stageScore.stars = Random.Range(1, 3);
		}
		instance.scoreDAO.maxStageReached++;
		Init();
	}

	public void OnEnable()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		tableTennisMainNavigation.characterView.ShowUIBackground();
		tableTennisMainNavigation.characterView.ShowCarbonPanel(false);
		tableTennisMainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.CoinsCashEnergy);
		StartCoroutine(DoScaleToWidth(3));
		if (shouldShowFreeAppWelcome)
		{
			shouldShowFreeAppWelcome = false;
			if (Ads.instance.isPromotedUser && !string.IsNullOrEmpty(ConfigBase.instance.promotionMessage))
			{
				TableTennisDialog.instance.ShowOk("Welcome!", ConfigBase.instance.promotionMessage, "Ok", null);
			}
		}
		GGPlayerSettings instance = GGPlayerSettings.instance;
		UITools.SetActive(multiplayerArrow, false);
	}

	private IEnumerator DoScaleToWidth(int times)
	{
		while (times > 0)
		{
			times--;
			Vector3[] wc = scrollView.GetComponent<UIPanel>().worldCorners;
			Bounds bounds = NGUIMath.CalculateAbsoluteWidgetBounds(container);
			Debug.Log(string.Concat("Bounds ", bounds.max, " min ", bounds.min));
			float boundsWidth = (bounds.max.x - bounds.min.x) / Mathf.Max(container.localScale.x, 0.01f);
			float panelWidth = Mathf.Abs(wc[2].x - wc[1].x);
			float scale = Mathf.Abs(panelWidth / boundsWidth);
			Debug.Log("panel width " + panelWidth);
			Vector3 localScale = container.localScale;
			localScale.x = scale;
			localScale.y = scale;
			container.localScale = localScale;
			Vector3 pos = container.position;
			pos.x = wc[0].x - bounds.min.x + pos.x;
			container.position = pos;
			yield return null;
		}
	}

	public void OnDisable()
	{
		TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		if (tableTennisMainNavigation != null)
		{
			tableTennisMainNavigation.characterView.ShowCarbonPanel(true);
		}
	}

	public void ResetCareer()
	{
		PlayerCareer instance = SingletonInit<PlayerCareer>.Instance;
		instance.scoreDAO.maxStageReached = 0;
		instance.scoreDAO.stageScores = null;
		instance.progressDAO.stageProgress = null;
		instance.Save();
		GGPlayerSettings instance2 = GGPlayerSettings.instance;
		instance2.Model.coins = 25;
		instance2.Save();
		Init();
	}

	public void StartTutorial()
	{
		MatchParameters.MatchParams p = MatchParameters.MatchParams.CreateTutorialParams();
		BehaviourSingleton<ScreenNavigation>.instance.LoadTableTennisMatch(p);
	}

	public Transform GetTransformForStage(int stage)
	{
		if (stage >= buttons.Count)
		{
			return null;
		}
		return buttons[stage].transform;
	}
}
